<?php
declare (strict_types = 1);

namespace app\index\controller;

use app\common\model\NewsModel;
use app\common\model\UrlModel;
use think\Controller;
use think\facade\View;
use think\facade\Db;
use think\facade\Log;

class Search extends Common
{
    public function index(){
        $keywords = input('keyword');
        
	    $head_id = $keywords;
	    View::assign('head_id',$head_id);
        if(!$keywords){
            return view('index',['searchres' => '', 'keywords'=>'暂无数据', 'keyword' =>'写点啥，不然没法搜!', 'count'=>'', 'page' =>'']);
        }
        $search = ['query'=>[]];
        $search['query']['keyword'] = $keywords;
        $where = [['source','like',"%{$keywords}%"],['keywords','like',"%{$keywords}%"],['title','like',"%{$keywords}%"]];
        $searchres = NewsModel::where('status',1)
            ->where(function($query) use ($where){$query->whereOr($where);})
            ->order('id','desc')
            ->paginate(['list_rows'=>6,false,'query' => request()->param()]);
        foreach($searchres as $key=>$val){
            if($val->id_domain == ''){
                $val->url = UrlModel::getShowUrl($val->id);
            }else{
                $val->url = UrlModel::getShowUrl($val->id_domain);
            }
	    }
        $page = $searchres->render();
        $count = $searchres->total();
        if($searchres->isEmpty()){
            return view('index',['searchres' => '', 'keywords'=>'暂无数据', 'keyword' => '暂无与《'.$keywords.'》相关的文章', 'count'=>$count, 'page' => $page]);
        }else{
            return view('index',['searchres' => $searchres, 'keywords'=>$keywords, 'count'=>$count, 'page' => $page]);
        } 
    }
    
    public function search(){
        error_reporting(E_ALL ^ E_DEPRECATED);
    	header('Content-type: text/html; charset=UTF-8');
    	if(empty($_GET['k'])||$_GET['k']==''){
    		die("<p>写点啥，不然没法搜！</p>");
    	}
    	$k = trim($_GET['k']);
    	$k = addslashes(htmlspecialchars(urldecode($k)));
    	$k = str_replace(array('%', '_'), array('\%', '\_'), $k);
        $myconn=mysqli_connect(env('database.hostname', ''),env('database.username', ''),env('database.password', ''));
        mysqli_query($myconn,"set names 'utf8'");
        mysqli_select_db($myconn,env('database.database', ''));
        $sql="select * from ".env('database.prefix', '')."news WHERE title Like '%$k%' and status='1' Or content Like '%$k%' and status='1' Order by id DESC limit 0,8"; 
    	$result=mysqli_query($myconn,$sql); 
    	$zs=mysqli_num_rows($result);
    	if($zs==0){
    		echo("<p>哈哈哈，站内啥也没有~</p>");
    	}else{
    		while($row=mysqli_fetch_array($result)){
    		    if($row["id_domain"] == ''){
    		        echo("<a href=\"".UrlModel::getShowUrl($row["id"])."\" title=\"".$row["title"]."\">".str_replace($k,"<span>".$k."</span>",$row["title"])."</a>");
    		    }else{
    		        echo("<a href=\"".UrlModel::getShowUrl($row["id_domain"])."\" title=\"".$row["title"]."\">".str_replace($k,"<span>".$k."</span>",$row["title"])."</a>");
    		    }
    		}
    	}
    	if($zs>1){
    		echo("<a href=\"".'/search.html'."?keyword=".$k."\" class=\"more\"><i class='layui-icon layui-icon-search'></i> 按文章标题搜索...</a>");
    	} 
    }
    
}
